/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bashe.business.adapter.webservices;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import han.bashe.business.adapter.Params;
import han.bashe.common.converter.Soap11Converter;
import han.bashe.common.converter.Soap12Converter;
import han.bashe.common.exception.BasheErrorCode;
import han.bashe.common.exception.BasheException;
import han.bashe.common.message.NormalizedMessage;
import v1.query.web.bifang.han.WebServicesBindingDocument.WebServicesBinding;

/**
 * @Author Kent
 */
public class WebServicesAdapter {
	
	public static NormalizedMessage call(NormalizedMessage req, WebServicesBinding b,String soapAction,
			Params params) throws BasheException {
		try {
			PostMethod post = null;

			HttpClient httpclient = new HttpClient();

			post = new PostMethod(b.getEndpointUrl());
			

			HttpClientParams ps = new HttpClientParams();
			ps.setSoTimeout(params.getTimeout() * 1000);
			httpclient.setParams(ps);
			String soap = null;
			if (b.getSoapVersion().equals("1.1")) {
				soap = Soap11Converter.toSoap(req);
			} else {
				soap = Soap12Converter.toSoap(req);
			}
			if(soapAction != null){
				post.addRequestHeader("SOAPAction", soapAction);
			}
			Log logger = LogFactory.getLog(WebServicesAdapter.class);
			logger.info("request xml is \n" + soap);
			RequestEntity entity = new StringRequestEntity(soap, "text/xml",
					"UTF-8");

			post.setRequestEntity(entity);

			int result = httpclient.executeMethod(post);
			String resxml = post.getResponseBodyAsString();
			logger.info("response xml is \n" + resxml);
			if (result != 200) {
				throw new BasheException(BasheErrorCode.CALL_HTTP_ERROR,
						"Server returns HTTP code " + result
								+ " \n response string is \n" + resxml);
			}
			NormalizedMessage res = null;
			if (b.getSoapVersion().equals("1.1")) {
				res = Soap11Converter.toNormalizedRes(resxml);
			} else {
				res = Soap12Converter.toNormalizedRes(resxml);
			}
			return res;
		} catch (Exception e) {
			throw new BasheException(BasheErrorCode.UNKNOWN_ERROR,
					"unknown error", e);
		}

	}

}
